package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.common.seller.aop.IgnoreSeller;
import com.catering.common.seller.context.SellerContextHolder;
import com.ruoyi.system.domain.Seller;
import com.ruoyi.system.domain.dto.ImageDTO;
import com.ruoyi.system.domain.dto.SellerFileBasicInfoDTO;
import com.ruoyi.system.domain.dto.SellerFileDTO;
import com.ruoyi.system.domain.dto.SellerFileStoreInfoDTO;
import com.ruoyi.system.domain.dto.SellerFileStoreInfoSelectAreaDTO;
import com.ruoyi.system.mapper.SellerMapper;
import com.ruoyi.system.service.CategoryService;
import com.ruoyi.system.service.SellerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 卖家服务impl
 *
 * @author Administrator
 * @date 2023/03/03
 */
@Service
public class SellerServiceImpl extends ServiceImpl<SellerMapper, Seller> implements SellerService {

    @Resource
    private CategoryService categoryService;

    @Override
    @IgnoreSeller
    public SellerFileDTO getStoreFile() {
        Seller entity = this.getById(SellerContextHolder.getRequiredSellerId());
        SellerFileDTO dto = new SellerFileDTO();
        if (ObjectUtil.isNotNull(entity)) {
            dto.setId(entity.getId());
            if (ObjectUtil.isNotNull(entity.getBusinessHour())) {
                dto.setBusinessHour(entity.getBusinessHour());
            } else {
                dto.setBusinessHour(0);
            }
            dto.setBusinessStatus(entity.getBusinessStatus());

            SellerFileStoreInfoDTO storeInfo = new SellerFileStoreInfoDTO();
            BeanUtils.copyProperties(entity, storeInfo);
            List<String> adcodes = new LinkedList<>();
            if (ObjectUtil.isNotNull(entity.getProvinceCode())) {
                adcodes.add(entity.getProvinceCode().toString());
            }
            if (ObjectUtil.isNotNull(entity.getCityCode())) {
                adcodes.add(entity.getCityCode().toString());
            }
            if (ObjectUtil.isNotNull(entity.getCountryCode())) {
                adcodes.add(entity.getCountryCode().toString());
            }
            storeInfo.setAdcodes(adcodes);
            List<Long> category = categoryService
                .getLevelArrayInfoByChildrenId(entity.getCategory());
            if (CollUtil.isNotEmpty(category)) {
                storeInfo.setCategory(category);
            }
            SellerFileStoreInfoSelectAreaDTO selectArea = new SellerFileStoreInfoSelectAreaDTO();
            if (StrUtil.isNotBlank(entity.getAddress())) {
                String[] strs = entity.getAddress().split(",");
                if (strs.length == 2) {
                    selectArea.setAddressDetails(strs[1]);
                }
                selectArea.setStreet(strs[0]);
            }
            storeInfo.setSelectArea(selectArea);
            dto.setStoreInfo(storeInfo);

            SellerFileBasicInfoDTO basicInfo = new SellerFileBasicInfoDTO();
            BeanUtils.copyProperties(entity, basicInfo);
            if (StrUtil.isNotBlank(entity.getRestaurantFeatures())) {
                basicInfo
                    .setRestaurantFeatures(
                        new ArrayList<>(Arrays.asList(entity.getRestaurantFeatures().split(","))));
            }
            if (StrUtil.isNotBlank(entity.getEnvUrl())) {
                basicInfo.setEnvUrl(JSON.parseArray(entity.getEnvUrl(), ImageDTO.class));
            } else {
                basicInfo.setEnvUrl(new ArrayList<>());
            }
            dto.setBasicInfo(basicInfo);
        }

        return dto;
    }
}
